<template>
	<view>
		<!--header-->
		
		<!--header-->
		<view class="tui-mybg-box" style="background-color: #ffcc00;">
			<image src="/static/images/mall/my/img_bg_3x.png" class="tui-my-bg" mode="widthFix" style="display: none;"></image>
			<block v-show="is_show">
			<view class="tui-header-center" v-if="userinfo">
				<image :src="userinfo.avatar" class="tui-avatar" @tap="href(3)"></image>
				<view class="tui-info">
					<view class="tui-nickname">{{userinfo.user_nickname}}</view>
				</view>
				<view class="text-center">
					<view class="mb-1 text-white font">积分：{{userinfo.score}}</view>
				</view>
				
			</view>
			<view class="tui-header-center" v-else>
				<image src="/static/images/my/mine_def_touxiang_3x.png" class="tui-avatar"></image>
				<view class="tui-info">
					<view class="tui-nickname">您还没有登录</view>
					
					<button class="tui-btn tui-btn-mini tui-primary tui-fillet" hover-class="tui-outline-hover"  @click="onGotUserInfo">授权登录</button>
					<!-- <button type="primary" open-type="getUserInfo" @getuserinfo="onGotUserInfo">授权登录</button> -->
				</view>
			</view>
			</block>
		</view>
		<view class="tui-content-box">
			<view class="tui-box tui-order-box">
				<tui-list-cell :arrow="true" padding="0" :lineLeft="false" @click="href(4,0)">
					<view class="tui-cell-header">
						<view class="tui-cell-title">我的订单</view>
						<view class="tui-cell-sub">查看全部订单</view>
					</view>
				</tui-list-cell>
				<view class="tui-order-list">
					<view class="tui-order-item" @tap="href(4,0)">
						<view class="tui-icon-box">
							<image src="/static/images/mall/my/icon_daifukuan_3x.png" class="tui-order-icon"></image>
							<view class="tui-badge tui-badge-red" v-if="info.no_pay">{{info.no_pay}}</view>
						</view>
						<view class="tui-order-text">全部清单</view>
					</view>
					<view class="tui-order-item" @tap="href(4,1)">
						<view class="tui-icon-box">
							<image src="/static/images/mall/my/icon_daifahuo_3x.png" class="tui-order-icon"></image>
							<view class="tui-badge tui-badge-red" v-if="info.no_shipping">{{info.no_shipping}}</view>
						</view>
						<view class="tui-order-text">待支付</view>
					</view>
					<view class="tui-order-item" @tap="href(4,2)">
						<view class="tui-icon-box">
							<image src="/static/images/mall/my/icon_daishouhuo_3x.png" class="tui-order-icon"></image>
							<view class="tui-badge tui-badge-red" v-if="info.no_getting">{{info.no_getting}}</view>
						</view>
						<view class="tui-order-text">待确认</view>
					</view>
					<view class="tui-order-item" @tap="href(4,3)">
						<view class="tui-icon-box">
							<image src="/static/images/mall/my/icon_pingjia_3x.png" class="tui-order-icon"></image>
							<view class="tui-badge tui-badge-red" v-if="info.no_comment">{{info.no_comment}}</view>
						</view>
						<view class="tui-order-text">已完成</view>
					</view>
				</view>
			</view>
			
			<view class="tui-set-box">
				
				<view class="tui-mtop">
					<!-- <tui-list-cell padding="0" :lineLeft="false" :arrow="true" @tap="detail('/pages/mall-extend/shopcart/shopcart')">
						<view class="tui-list-cell">
							购物车
						</view>
					</tui-list-cell> -->

					<tui-list-cell padding="0" :lineLeft="false" :arrow="true" @tap="detail('/pages/coupon/coupon?type=0&cou=0')">
						<view class="tui-list-cell">
							优惠券
						</view>
					</tui-list-cell> 

				</view>
				<view class="tui-mtop">
					<tui-list-cell padding="0" :lineLeft="false" :arrow="true" @tap="href(3)">
						<view class="tui-list-cell">
							下级用户
						</view>
					</tui-list-cell>
					<tui-list-cell padding="0" :lineLeft="false" :arrow="true">
						<button class="contact" open-type="contact">
						<view class="tui-list-cell">
							客服服务
						</view>
						</button>
					</tui-list-cell>
					<tui-list-cell padding="0" :lineLeft="false" :arrow="true" @tap="detail('/pages/mall-extend/add/add?type=2')">
						<button class="contact">
						<view class="tui-list-cell">
							参团人员
						</view>
						</button>
					</tui-list-cell>
				</view>
				<block v-if="userinfo">
				<view class="tui-exit">
					<tui-button type="danger" @tap="logout" height="88rpx" style="background:#ffcc00 !important">退出登录</tui-button>
				</view>
				</block>
			</view>
			
		</view>
		
		<!-- 充值弹框 -->
		
		<view class="tui-tabbar">
			<block v-for="(item,index) in tabbar" :key="index">
				<view class="tui-tabbar-item" :class="[current==index?'tui-item-active':'']" :data-index="index" @tap="tabbarSwitch">
					<view :class="[index==0?'tui-ptop-4':'']">
						<image :src="item.images" mode="" style="width:60rpx;height:60rpx"></image>
					</view>
					<view class="tui-scale">{{item.text}}</view>
				</view>
			</block>
		</view>
		
	</view>
</template>

<script>
	var api = require('@/common/api.js')
	import tuiIcon from "@/components/icon/icon"
	import tuiButton from "@/components/extend/button/button"
	import tuiListCell from "@/components/list-cell/list-cell"
	import tuiDivider from "@/components/divider/divider"
	import tuiLoadmore from "@/components/loadmore/loadmore"
	import tuiModal from "@/components/modal/modal"
	export default {
		components: {
			tuiIcon,
			tuiButton,
			tuiListCell,
			tuiDivider,
			tuiLoadmore,
			tuiModal
		},
		onLoad: function(options) {
			if(this.tui.isLogin()){
				this.userinfo = uni.getStorageSync("user")
			}else{
				uni.showToast({
					title:"您没有登陆，请先登录",
					icon:"none"
				})
			}
			let obj = {};
			obj = wx.getMenuButtonBoundingClientRect();

			uni.getSystemInfo({
				success: (res) => {
					this.width = obj.left || res.windowWidth;
					this.height = obj.top ? (obj.top + obj.height + 8) : (res.statusBarHeight + 44);
					this.top = obj.top ? (obj.top + (obj.height - 32) / 2) : (res.statusBarHeight + 6);
					this.scrollH = res.windowWidth * 0.6
				}
			})
			

			this.is_show = true
			//获取用户的订单信息
			
			if(options.prentid){
				this.father_id = options.prentid
			}
			console.log(uni.getStorageSync("father"));
			if(uni.getStorageSync("father")){
				this.father_id = uni.getStorageSync("father")
			}
			

		},
		data() {
			return {
				height: 64, //header高度
				top: 0, //标题图标距离顶部距离
				scrollH: 0, //滚动总高度
				opcity: 0,
				iconOpcity: 0.5,
				tabbar: [{
					text: "首页",
					images:"/static/images/lv/5.png"
				}, {
					text: "积分领取",
					images:"/static/images/lv/6.png"
				}, {
					text: "热门活动",
					images:"/static/images/lv/7.png"
				}, {
					text: "我的",
					images:"/static/images/lv/8.png"
				}],
				current:3,
				userinfo:'',
				info:{},
				is_show:false,
				is_staff:false,
				modal:false,
				money:'',
				login:'',
				father_id:''
			}
		},
		methods: {
			//
			showModal:function(){
				this.modal = true;
			},
			//微信小程序登录
			onGotUserInfo() {
				
				uni.getUserProfile({
					desc:'登录',
					success: res => {
						console.log(res.iv)
						uni.login({
							success: loginRes => {
								if(loginRes.code){
									api.post({
										url: 'wxapp/public/login',
										data: {
											code: loginRes.code,
											encrypted_data: res.encryptedData,
											iv: res.iv,
											raw_data: res.rawData,
											signature: res.signature
										},
										success: data => {
											
											console.log('用户数据',data)
											this.userinfo = data.data.user;
											if(uni.getStorageSync("father")){
												api.get({
													url:"order/fatherid",
													data:{
														id:this.userinfo.id,
														user_id:uni.getStorageSync("father"),
													},
													success: (sj) => {
														console.log(sj)
													}
													
												})
											}
											if (data.code == 1) {
												uni.showToast({
													title: '登录成功!',
													icon: 'success',
													duration: 500
												});
												try {
													uni.setStorageSync('upload', 1)
													uni.setStorageSync('login', 1)
													uni.setStorageSync('token', data.data.token)
													uni.setStorageSync('user', data.data.user)
												} catch (e) {}
												setTimeout(function() {
													uni.redirectTo({
														url:"/pages/my/my"
													})
												}, 500)
											}
										}
									});
								}
						
							}
						})
						
						
					},
					fail: (res) => {
						if (res.errMsg == "getUserInfo:cancel" || res.errMsg == "getUserInfo:fail auth deny") {
							uni.showModal({
								title: '用户授权失败',
								showCancel: false,
								content: '请删除此小程序重新授权!',
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确定')
									}
								}
							})
						}
				
					},
					complete:e=>{
						console.log('完成信息',e)
					}
				})
			},
			href(page,index) {
				let url = "";
				switch (page) {
					case 1:
						break;
					case 2:
						url = "../set/set"
						break;
					case 3:
						url = "/pages/category/category"
						break;
					case 4:
						url = "../myOrder/myOrder?index="+index
						break;
					case 5:
						url = "/pages/download/download"
						break;
					default:
						break;
				}
				if(url){
					uni.navigateTo({
						url: url
					})
				}else{
					this.tui.toast("功能尚未完善~")
				}
			},
			detail(page) {
				uni.navigateTo({
					url: page
				})
			},

			pay:function(order_sn){
				let that = this;
				uni.login({
				  provider: 'weixin',
				  success: function (res) {
					if(res.code){
						api.post({
							url:"pay/wxpay",
							data:{
								code:res.code,
								order_sn:order_sn
							},
							success: res=>{
								var info = res.data;
								//调用微信支付api
								 uni.requestPayment({
									 provider: 'wxpay',
									 timeStamp: info.timeStamp,
									 nonceStr: info.nonceStr,
									 package: info.package,
									 signType: info.signType,
									 paySign: info.paySign, //签名，这个要提前获取一下
									 success: function (res) {
										uni.showToast({
											 title:'支付成功',
											 icon:"success"
										})
										that.userinfo.balance = parseFloat(that.userinfo.balance) + parseFloat(that.money)
										uni.setStorageSync('user',that.userinfo);
									 },
									 fail: function (err) {
										 uni.showToast({
											 title:'支付失败',
											 icon:'none'
										 })
									 }
								 }) 
							}
						})
					}
				  },
				})
				
			},
			tabbarSwitch: function(e) {
				let index = e.currentTarget.dataset.index;
				this.current = index;
				if (index != 3) {
					if (index == 1) {
						uni.redirectTo({
							url: '/pages/download/download'
						})
					} else if (index == 2) {
						uni.redirectTo({
							url: '/pages/mall/mall'
						})
					} else if(index == 0){
						uni.redirectTo({
							url: '/pages/index/index'
						})
					}
				}
			},
			logout(){
				// 询问是否退出登录？
				uni.showModal({
					content:'是否要退出登录',
					success:(res) => {
						if (res.confirm) {
							uni.clearStorageSync();
							this.userinfo="";
							uni.navigateBack({
								delta:1
							})
							uni.showToast({
								title:"退出登录成功",
								icon:'none'
							})
						}
					}
				})
			}
		},
		onPageScroll(e) {
			let scroll = e.scrollTop <= 0 ? 0 : e.scrollTop;
			let opcity = scroll / this.scrollH;
			if (this.opcity >= 1 && opcity >= 1) {
				return;
			}
			this.opcity = opcity;
			this.iconOpcity = 0.5 * (1 - opcity < 0 ? 0 : 1 - opcity)
		},
		onPullDownRefresh() {
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 200)
		},
		onReachBottom: function() {
			if (!this.pullUpOn) return;
			this.loadding = true;
			if (this.pageIndex == 4) {
				this.loadding = false;
				this.pullUpOn = false
			} else {
				let loadData = JSON.parse(JSON.stringify(this.productList));
				loadData = loadData.splice(0, 10)
				if (this.pageIndex == 1) {
					loadData = loadData.reverse();
				}
				this.productList = this.productList.concat(loadData);
				this.pageIndex = this.pageIndex + 1;
				this.loadding = false
			}
		}
	}
</script>

<style>
	.contact {
		background: transparent;
		border: none;
		padding: 0;
		margin: 0;
		line-height: 1.5;
		width: 100%;
	}
	.button-hover {
		background: transparent;
	}
	/*圆角 */
	
	.tui-fillet {
		border-radius: 45rpx;
	}
	
	.tui-white.tui-fillet::after {
		border-radius: 90rpx;
	}
	
	.tui-outline-fillet::after {
		border-radius: 90rpx;
	}
	.tui-btn{
		margin-bottom: 26upx;
		margin-right: 20upx;
	}
	
	.tui-btn-mini {
		width: auto;
		font-size: 30rpx;
		height: 70rpx;
		line-height: 70rpx;
	}
	
	.tui-primary {
		background: #5677fc !important;
		color: #fff;
	}
	
	/*tabbar*/
	
	.tui-tabbar {
		width: 100%;
		position: fixed;
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 99999;
		background: #fff;
		height: 125rpx;
		left: 0;
		bottom: 0;
		padding-bottom: env(safe-area-inset-bottom);
	}
	
	.tui-safearea-bottom {
		width: 100%;
		height: env(safe-area-inset-bottom);
	}
	
	.tui-tabbar::before {
		content: '';
		width: 100%;
		border-top: 1rpx solid #d9d9d9;
		position: absolute;
		top: 0;
		left: 0;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
	}
	
	.tui-tabbar-item {
		flex: 1;
		width: 25%;
		display: flex;
		align-items: center;
		flex-direction: column;
		justify-content: space-between;
		font-size: 24rpx;
		color: #848484;
		height: 80rpx;
	}
	
	.tui-ptop-4 {
		padding-top: 4rpx;
	}
	
	.tui-scale {
		font-weight: bold;
		transform: scale(0.8);
		transform-origin: center 100%;
		line-height: 30rpx;
	}
	
	.tui-item-active {
		color: #f28e26 !important;
	}
	
	/*tabbar*/
	
	.tui-header-box {
		width: 100%;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 9998;
	}

	.tui-header {
		width: 100%;
		font-size: 18px;
		line-height: 18px;
		font-weight: 500;
		height: 32px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	/* #ifndef MP */
	.tui-header-icon {
		position: fixed;
		top: 0;
		right: 15px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 32px;
		transform: translateZ(0);
		z-index: 99999;
	}

	/* #endif */
	/* #ifdef MP */
	.tui-set-box {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	/* #endif */
	.tui-icon-box {
		position: relative;
	}

	.tui-icon-setup {
		margin-left: 8rpx;
	}

	.tui-badge {
		position: absolute;
		font-size: 24rpx;
		height: 32rpx;
		min-width: 20rpx;
		padding: 0 6rpx;
		border-radius: 40rpx;
		right: 10rpx;
		top: -5rpx;
		transform: scale(0.8) translateX(60%);
		transform-origin: center center;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 10;
	}

	.tui-badge-red {
		background: #F74D54;
		color: #fff;
	}

	.tui-badge-white {
		background: #fff;
		color: #F74D54;
	}

	.tui-badge-dot {
		position: absolute;
		height: 12rpx;
		width: 12rpx;
		border-radius: 50%;
		right: -12rpx;
		top: 0;
		background: #F74D54;
	}

	.tui-mybg-box {
		width: 100%;
		height: 400rpx;
		position: relative;
	}

	.tui-my-bg {
		width: 100%;
		height: 364rpx;
		display: block;
	}

	.tui-header-center {
		position: absolute;
		width: 100%;
		height: 128rpx;
		left: 0;
		top: 160rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.tui-avatar {
		flex-shrink: 0;
		width: 128rpx;
		height: 128rpx;
		display: block;
		border-radius: 128rpx;
	}

	.tui-info {
		width: 60%;
		padding-left: 30rpx;

	}

	.tui-nickname {
		font-size: 30rpx;
		font-weight: 500;
		color: #fff;
		display: flex;
		align-items: center;
		padding: 12rpx;
	}

	.tui-img-vip {
		width: 56rpx;
		height: 24rpx;
		margin-left: 18rpx;
	}

	.tui-explain {
		width: 80%;
		font-size: 24rpx;
		font-weight: 400;
		color: #fff;
		opacity: 0.75;
		padding-top: 8rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.tui-btn-edit {
		flex-shrink: 0;
		padding-right: 22rpx;
	}

	.tui-header-btm {
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;
		position: absolute;
		left: 0;
		top: 280rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		color: #fff;
	}

	.tui-btm-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.tui-btm-num {
		font-size: 32rpx;
		font-weight: 600;
		position: relative;
	}

	.tui-btm-text {
		font-size: 24rpx;
		opacity: 0.85;
		padding-top: 4rpx;
	}

	.tui-content-box {
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;
		position: relative;
		top: -72rpx;
		z-index: 10;
	}

	.tui-box {
		width: 100%;
		background: #fff;
		box-shadow: 0 3rpx 20rpx rgba(183, 183, 183, 0.1);
		border-radius: 10rpx;
		overflow: hidden;
	}

	.tui-order-box {
		height: 208rpx;
	}

	.tui-cell-header {
		width: 100%;
		height: 74rpx;
		padding: 0 26rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.tui-cell-title {
		font-size: 30rpx;
		line-height: 30rpx;
		font-weight: 600;
		color: #333;
	}

	.tui-cell-sub {
		font-size: 26rpx;
		font-weight: 400;
		color: #999;
		padding-right: 28rpx;
	}

	.tui-order-list {
		width: 100%;
		height: 134rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;

	}

	.tui-order-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.tui-order-text,
	.tui-tool-text {
		font-size: 26rpx;
		font-weight: 400;
		color: #666;
		padding-top: 4rpx;
	}

	.tui-tool-text {
		font-size: 24rpx;
	}

	.tui-order-icon {
		width: 56rpx;
		height: 56rpx;
		display: block;
	}

	.tui-assets-box {
		height: 178rpx;
		margin-top: 20rpx;
	}

	.tui-assets-list {
		height: 84rpx;
	}

	.tui-assets-num {
		font-size: 32rpx;
		font-weight: 500;
		color: #333;
		position: relative;
	}

	.tui-assets-text {
		font-size: 24rpx;
		font-weight: 400;
		color: #666;
		padding-top: 6rpx;
	}

	.tui-tool-box {
		margin-top: 20rpx;
	}

	.tui-flex-wrap {
		flex-wrap: wrap;
		height: auto;
		padding-bottom: 30rpx;
	}

	.tui-tool-item {
		width: 25%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		padding-top: 30rpx;
	}

	.tui-tool-icon {
		width: 64rpx;
		height: 64rpx;
		display: block;
	}

	.tui-badge-icon {
		width: 66rpx;
		height: 30rpx;
		position: absolute;
		right: 0;
		transform: translateX(88%);
		top: -15rpx;
	}

	/*为你推荐*/
	.tui-product-list {
		display: flex;
		justify-content: space-between;
		flex-direction: row;
		flex-wrap: wrap;
		box-sizing: border-box;
	}

	.tui-product-container {
		flex: 1;
		margin-right: 2%;
	}

	.tui-product-container:last-child {
		margin-right: 0;
	}

	.tui-pro-item {
		width: 100%;
		margin-bottom: 4%;
		background: #fff;
		box-sizing: border-box;
		border-radius: 12rpx;
		overflow: hidden;
	}

	.tui-pro-img {
		width: 100%;
		display: block;
	}

	.tui-pro-content {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 20rpx;
	}

	.tui-pro-tit {
		color: #2e2e2e;
		font-size: 26rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.tui-pro-price {
		padding-top: 18rpx;
	}

	.tui-sale-price {
		font-size: 34rpx;
		font-weight: 500;
		color: #e41f19;
	}

	.tui-factory-price {
		font-size: 24rpx;
		color: #a0a0a0;
		text-decoration: line-through;
		padding-left: 12rpx;
	}

	.tui-pro-pay {
		padding-top: 10rpx;
		font-size: 24rpx;
		color: #656565;
	}
	
	.tui-set-box {
		padding-bottom: 20rpx;
		color: #333;
		display: block;
	}
	
	.tui-list-cell {
		display: flex;
		align-items: center;
		padding: 24rpx 30rpx;
		font-size: 30rpx;
	}
	
	.tui-info-box {
		font-size: 34rpx;
	}
	
	.tui-avatar {
		width: 140rpx;
		height: 140rpx;
		margin-right: 20rpx;
	}
	
	.tui-mtop {
		margin-top: 20rpx;
	}
	
	.tui-exit {
		padding: 100rpx 24rpx;
	}
</style>
